﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WxApiLib.Messages;

namespace WxApiLib.Handlers
{
    /// <summary>
    /// 文本信息处理类
    /// </summary>
    public class TextHandler : IHandler
    {
        /// <summary>
        /// 请求的XML
        /// </summary>
        private string RequestXml { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="requestXml">请求的xml</param>
        public TextHandler(string requestXml)
        {
            this.RequestXml = requestXml;
        }
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <returns></returns>
        public string HandleRequest()
        {
            string response = string.Empty;
            TextMessage tm = TextMessage.LoadFromXml(RequestXml);
            string content = tm.Content.Trim();
            if (string.IsNullOrEmpty(content))
            {
                response = "您什么都没输入，没法帮您啊，%>_<%。";
            }
            else
            {
                //天气功能
                if (content.StartsWith("tq", StringComparison.OrdinalIgnoreCase))
                {
                    string cityName = content.Substring(2).Trim();
                    //response = WeatherHelper.GetWeather(cityName);
                }
                else
                {
                    response = HandleOther(content);
                }
            }
            tm.Content = response;
            //进行发送者、接收者转换
            string temp = tm.ToUserName;
            tm.ToUserName = tm.FromUserName;
            tm.FromUserName = temp;
            response = tm.GenerateContent();
            return response;
        }
        /// <summary>
        /// 处理其他消息
        /// </summary>
        /// <param name="tm"></param>
        /// <returns></returns>
        private string HandleOther(string requestContent)
        {
            string response = string.Empty;
            if (requestContent.Contains("你好") || requestContent.Contains("您好"))
            {
                response = "你也好!";
            }
            else if (requestContent.Contains("傻"))
            {
                response = "我不傻！哼~ ";
            }
            else if (requestContent.Contains("逼") || requestContent.Contains("操"))
            {
                response = "哼，你说脏话！ ";
            }
            else if (requestContent.Contains("是谁"))
            {
                response = "连我都不认识？";
            }
            else if (requestContent.Contains("再见"))
            {
                response = "再见！";
            }
            else if (requestContent.Contains("bye"))
            {
                response = "Bye！";
            }
            else if (requestContent.Contains("谢谢"))
            {
                response = "不客气！嘿嘿";
            }
            else
            {
                response = "您说的，可惜，我没明白啊，试试其他关键字吧。";
            }

            return response;
        }
    }
}
